﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using ZhimeiSoft.Framework.Client.Connector.Manager;
using ZhimeiSoft.Framework.Common.Entity;
using ZhimeiSoft.Framework.Client.Utils;

namespace ZhimeiSoft.Framework.Client.Base
{
    public class LoginFormBase : Form, ILoginForm
    {
        private LoginEntity output;

        public LoginFormBase()
        {
            base.StartPosition = FormStartPosition.CenterScreen;
            this.Text = "登录";
            //base.Icon = ApplicationHelper.MainIcon;
            //Image image = ResourceHelper.GetImage("login");
            //if (image != null)
            //{
            //    this.BackgroundImage = image;
            //}
        }
        

        public void Execute()
        {
            throw new NotImplementedException();
        }

        protected void Submit(string username, string password)
        {
            int errorCode = 0;

            LoginEntity entity = new LoginEntity();
            entity.UserName = username;
            entity.Password = password;
            this.output = entity;
            /*base.DialogResult = DialogResult.OK;
            return;*/
            
            try
            {
                bool flag = ConnectorManager.Instance.Login(ref entity, out errorCode);
                
                if (flag)
                {
                    this.output = entity;
                    base.DialogResult = DialogResult.OK;
                }
                else
                {
                    switch (errorCode)
                    { 
                        case -1:
                            MsgBox.MsgInfo("用户不存在！");
                            break;
                        case -2:
                            MsgBox.MsgInfo("密码不正确！");
                            break;
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public LoginEntity Login(LoginEntity input)
        {
            LoginEntity result = null;

            if (base.ShowDialog() == DialogResult.OK)
            {
                result = this.output;
            }
            return result;
        }

        
    }
}
